package com.suse.service.admin.edu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.suse.common.config.response.ApiException;
import com.suse.common.config.response.R;
import com.suse.common.util.core.StringUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.suse.service.admin.edu.feign.CosService;
import com.suse.service.admin.edu.mapper.TeacherMapper;
import com.suse.service.admin.edu.model.entity.Course;
import com.suse.service.admin.edu.model.entity.Teacher;
import com.suse.service.admin.edu.model.param.TeacherQueryParam;
import com.suse.service.admin.edu.model.param.TeacherSaveParam;
import com.suse.service.admin.edu.model.param.TeacherUpdateParam;
import com.suse.service.admin.edu.model.vo.NameTeacherVo;
import com.suse.service.admin.edu.model.vo.SelectTeacherVo;
import com.suse.service.admin.edu.service.CourseService;
import com.suse.service.admin.edu.service.TeacherService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.suse.common.config.response.SimpleCode.*;


/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author 辜玉彬
 * @since 2022-08-09
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Resource
    private CosService cosService;

    @Resource
    private CourseService courseService;

    @Override
    public R getTeacherTable(TeacherQueryParam teacherQueryParam) {
        Long current = teacherQueryParam.getCurrent();
        Long size = teacherQueryParam.getSize();
        String name = teacherQueryParam.getName();
        Integer level = teacherQueryParam.getLevel();
        Page<Teacher> page = new Page<>(current,size);
        page = lambdaQuery().like(StringUtil.isNotEmpty(name),Teacher::getName,name)
                .eq(ObjectUtil.isNotNull(level),Teacher::getLevel,level)
                .orderByDesc(Teacher::getGmtCreate)
                .page(page);
        long total = page.getTotal();
        List<Teacher> list = page.getRecords();
        return R.put("total",total).put("records",list).success();
    }

    @Override
    public R removeTeacher(String id) {
        removeTeacherAvatar(id);
        if (removeById(id)) {
            return R.success(REMOVE_SUCCESS);
        }else {
            throw new ApiException(REMOVE_ERROR);
        }
    }

    private boolean removeTeacherAvatar(String id) {
        Teacher teacher = lambdaQuery().select(Teacher::getAvatar).eq(Teacher::getId,id).one();
        String avatar = teacher.getAvatar();
        // 判空
        if (ObjectUtil.isNull(teacher) || ObjectUtil.isNull(avatar)) {
            return false;
        }
        R r = cosService.removeFile(avatar);
        return r.getCode() == 200;
    }

    @Override
    public R save(TeacherSaveParam teacherSaveParam) {
        Teacher teacher = BeanUtil.copyProperties(teacherSaveParam, Teacher.class);
        if (save(teacher)) {
            return R.success(SAVE_SUCCESS);
        }else {
            throw new ApiException(SAVE_ERROR);
        }
    }

    @Override
    public R update(TeacherUpdateParam teacherUpdateParam) {
        Teacher teacher = BeanUtil.copyProperties(teacherUpdateParam, Teacher.class);
        if (updateById(teacher)) {
            return R.success(UPDATE_SUCCESS);
        }else {
            throw new ApiException(UPDATE_ERROR);
        }
    }

    @Override
    public R getTeacherSelect() {
        List<Teacher> teacherList = lambdaQuery().select(Teacher::getId, Teacher::getName).list();
        List<SelectTeacherVo> selectListTeacherViewList = BeanUtil.copyToList(teacherList, SelectTeacherVo.class);
        return R.success(selectListTeacherViewList);
    }

    @Override
    public R removeBatchTeacher(String[] ids) {
        if (removeByIds(CollectionUtil.toList(ids))) {
            return R.success("删除成功");
        }else {
            throw new ApiException("删除失败");
        }
    }

    @Override
    public R getTeacherNameListByKey(String key) {
        List<Teacher> teacherList = lambdaQuery().select(Teacher::getName).likeRight(Teacher::getName, key).list();
        List<NameTeacherVo> nameListVo = BeanUtil.copyToList(teacherList, NameTeacherVo.class);
        return R.success(nameListVo);
    }

    @Override
    public R getTeacherList() {
        return R.success(list());
    }

    @Override
    public R getTeacherInfoAndCourseById(String id) {
        Teacher teacher = getById(id);
        List<Course> courseList = courseService.lambdaQuery()
                .eq(Course::getTeacherId, id)
                .list();
        return R.put("teacher",teacher).put("courseList",courseList).success();
    }

}
